package net.toocruel.iqismart.web.controller.home.profile;

import net.toocruel.iqismart.entity.common.Category;
import net.toocruel.iqismart.entity.constants.CategoryVisibility;
import net.toocruel.iqismart.entity.core.Authentication;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.service.dto.ProfileAuthenticationDTO;
import net.toocruel.iqismart.service.dto.SessionMessage;
import net.toocruel.iqismart.service.helper.MessageHelp;
import net.toocruel.iqismart.utils.FileUploadUtils;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/14 下午2:12
 */
@Controller
@RequestMapping("profile")
public class ProfileAuthenticationController extends AbstractBaseController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private FileUploadUtils fileUploadUtils;

    @Autowired
    private AuthenticationService authenticationService;


    @GetMapping("authentication")
    public ModelAndView authentication(ProfileAuthenticationDTO dto){
        ModelAndView modelAndView = new ModelAndView(getTemplate("index"));
        modelAndView.addObject("dto",dto);
        modelAndView.addObject("edit",false);

        return modelAndView;
    }


    @PostMapping("authentication")
    public ModelAndView authentication(ProfileAuthenticationDTO dto, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        //
        ModelAndView modelAndView = new ModelAndView(getTemplate("index"));
        try {
            authenticationService.auth(getCurrentUser().getId(),dto);

            MessageHelp.putSessionMessage(new SessionMessage(SessionMessage.OK,"您的申请提交成功！我们会在三个工作日内处理，处理结果会通过邮件、站内消息方式通知您，如有疑问请联系官方管理员。"));
            refreshCurrentUser();
            modelAndView.setViewName("redirect:"+request.getRequestURI());
        }catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error",e);
        }

        modelAndView.addObject("dto",dto);
        modelAndView.addObject("edit",true);
        return modelAndView;
    }

    @GetMapping("authentication/edit")
    public ModelAndView editAuthentication(){
        ModelAndView modelAndView = new ModelAndView(getTemplate("index"));
        Authentication authentication = authenticationService.findById(getCurrentUser().getId());
        ProfileAuthenticationDTO dto = new ProfileAuthenticationDTO();
        dto.setCategory(authentication.getCategory());
        dto.setCategoryId(authentication.getCategory().getId());
        dto.setIdCard(authentication.getIdCard());
        dto.setCity(authentication.getCity());
        dto.setProvince(authentication.getProvince());
        dto.setTags(authentication.getTags());
        dto.setTitle(authentication.getTitle());
        dto.setDescription(authentication.getDescription());
        dto.setGender(authentication.getGender());
        dto.setRealName(authentication.getRealName());
        dto.setTagNames(authentication.getTagNames());
        modelAndView.addObject("dto",dto);
        modelAndView.addObject("edit",true);
        return modelAndView;
    }



    @ModelAttribute
    private Authentication authentication(){
        return authenticationService.findById(getCurrentUser().getId());
    }

    @ModelAttribute(name = "categorys")
    public List<Category> categorys(){
        List<Category> byVisibilityAndRoles = categoryService.findByVisibilityAndRoles(CategoryVisibility.experts, getCurrentUser());
        return byVisibilityAndRoles;
    }



    @Override
    protected String getModule() {
        return "authentication";
    }

    @ModelAttribute
    public User user(){
        return userService.getById(getCurrentUser().getId());
    }



}
